import axios from 'axios'
import { Loading } from 'element-ui';
import { getCookie } from '@/common/js/cookie';

import prefixAPI from "@/api/prefixAPI"; // prefixAPI 前缀
const fileUrl = prefixAPI.pack;

const mimeMap = {
  xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  zip: 'application/zip'
}

export function downLoadTemplate (str, fileName) {
  const tempLoading = Loading.service({
    lock: true,
    text: '导出中',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  });
  axios({
    method: 'GET',
    url: str,
    responseType: 'blob',
    headers: { 'Authorization': getCookie("token") }
  }).then(res => {
    resolveBlob(res, mimeMap.xlsx, fileName)
    tempLoading.close();
  }).catch(() => {
    tempLoading.close();
  })
}
/**
 * 解析blob响应内容并下载
 * @param {*} res blob响应内容
 * @param {String} mimeType MIME类型
 */
export function resolveBlob (res, mimeType, filename) {
  const aLink = document.createElement('a')
  var blob = new Blob([res.data], { type: mimeType })
  if (filename) {
    var fileName = filename
  } else {
    // //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
    var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
    var contentDisposition = decodeURI(res.headers['content-disposition'])
    var result = patt.exec(contentDisposition)
    var fileName = result[1]
    fileName = fileName.replace(/\"/g, '')
  }
  aLink.style.display = 'none'
  aLink.href = URL.createObjectURL(blob)
  aLink.setAttribute('download', fileName) // 设置下载文件名称
  document.body.appendChild(aLink)
  aLink.click()
  URL.revokeObjectURL(aLink.href);//清除引用
  document.body.removeChild(aLink);
}


export function downLoadExcelBody (str, data, filename,type='data') {
  const loading = Loading.service({
    lock: true,
    text: '导出中',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  });
  var url = fileUrl + str;
  let options = {
    method: 'post',
    url,
    responseType: 'blob',
    headers: { 'token': getCookie('token') },
    timeout: 300000
  }
  options[type] = data;
  axios(options).then(res => {
    resolveBlob(res, mimeMap.xlsx, filename)
    loading.close();
  }).catch(() => {
    loading.close();
  })
}

export function downLoadExcelParams (url, params, filename) {
  // console.log(filename)
  const loading = Loading.service({
    lock: true,
    text: '导出中',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  });
  var url = fileUrl + url
  axios({
    method: 'get',
    url: url,
    params,
    responseType: 'blob',
    headers: { 'token': getCookie('token') },
    timeout: 300000
  }).then(res => {
    resolveBlob(res, mimeMap.xlsx, filename)
    loading.close();
  }).catch(() => {
    loading.close();
  })
}

export function downLoadPptBody (str, data, filename) {
  const loading = Loading.service({
    lock: true,
    text: '导出中',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  });
  var url = fileUrl + str
  axios({
    method: 'post',
    url: url,
    data: data,
    responseType: 'blob',
    headers: { 'Authorization': 'Bearer ' + getToken() }
  }).then(res => {
    resolveBlob(res, mimeMap.pptx, filename)
    loading.close();
  }).catch(() => {
    loading.close();
  })
}